package com.gooddare.view.component 
{
	import com.gooddare.controller.ApplicationFacade;
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.ModelLocator;
	import com.gooddare.model.vo.GridviewItemVO;
	import com.riatimes.mvc.view.BaseComponent;
	import com.riatimes.util.Reflection;
	import com.riatimes.util.UIUtil;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import com.greensock.easing.Back;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quad;
	import com.greensock.TweenMax;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class GridViewItemComponent extends BaseComponent
	{
		private var vo:GridviewItemVO;
		private var gv:Sprite;
		private var background:Sprite;
		private var imgPanel:Sprite;
		private var infoPanel:Sprite;
		private var eff:Sprite;
		private var effMask:Sprite;
		private var loader:Loader;
		
		private var idTxt:TextField;
		private var titleTxt:TextField;
		private var infoTxt:TextField;
		private var dateTxt:TextField;
		private var iTxt:TextField;
		private var dTxt:TextField;
		private var picLoading:Sprite;
		private var pli:Sprite;
		private var plBack:Sprite;
		
		public var id:int;
		public var bimg:String;
		public var msg:String;
		public function GridViewItemComponent(id:int,vo:GridviewItemVO) 
		{
			this.vo = vo;
			this.id = id;
		}
		override public function initUI():void {
			gv = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_GRIDVIEW_UI) as Sprite;
			this.addChild(gv);
			
			picLoading = UIUtil.getElement(gv, "picLoading") as Sprite;
			pli = UIUtil.getElement(picLoading, "pli") as Sprite;
			plBack = UIUtil.getElement(picLoading, "plBack") as Sprite;
			
			background = UIUtil.getElement(gv, "background")as Sprite;
			imgPanel = UIUtil.getElement(gv, "imgPanel") as Sprite;
			infoPanel = UIUtil.getElement(gv, "infoPanel") as Sprite;
			eff = UIUtil.getElement(gv, "eff") as Sprite;
			effMask = UIUtil.getElement(gv, "effMask") as Sprite;
			eff.mask = effMask;
			
			idTxt = UIUtil.getElement(gv, "idTxt") as TextField;
			titleTxt = UIUtil.getElement(gv, "titleTxt") as TextField;
			infoTxt = UIUtil.getElement(gv, "infoTxt") as TextField;
			dateTxt = UIUtil.getElement(gv, "dateTxt") as TextField;
			iTxt = UIUtil.getElement(gv, "iTxt") as TextField;
			dTxt = UIUtil.getElement(gv, "dTxt") as TextField;
			
			idTxt.text = vo.id.toString();
			titleTxt.text = vo.title;
			infoTxt.text = vo.info;
			dateTxt.text = vo.datetime;

			this.filters = [new GlowFilter(0xffffff, 0, 0, 0, 1.5)];
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, __loaderProgressHandle);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __loaderCompleteHandle);
			loader.load(new URLRequest(vo.limg));
		}
		
		private function __loaderProgressHandle(e:ProgressEvent):void 
		{
			//pli.width = (e.bytesLoaded / e.bytesTotal) * plBack.width;
			TweenMax.to(pli, 0.5, { width:(e.bytesLoaded / e.bytesTotal) * plBack.width } );
		}
		
		private function __loaderCompleteHandle(e:Event):void 
		{
			TweenMax.to(picLoading, 0.3, { alpha:0 } );
			loader.width = imgPanel.width;
			loader.height = imgPanel.height;
			imgPanel.addChild(loader);
			loader.alpha = 0;
			TweenMax.to(loader, 0.3, {delay:0.1, alpha:1 } );
		}
		override public function initEvent (b:Boolean = true) : void {
			if (b) {
				this.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				this.addEventListener(MouseEvent.ROLL_OUT, __mouseRollEventHandle);
				this.addEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			}else {
				this.removeEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				this.removeEventListener(MouseEvent.ROLL_OUT, __mouseRollEventHandle);
				this.removeEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			}
		}
		
		private function __mouseClickEventHandle(e:MouseEvent):void 
		{
			this.bimg = vo.bimg;
			this.msg = vo.msg;
			ModelLocator.playerId = this.id;
			
			var facade:ApplicationFacade = ApplicationFacade.getInstance();
			facade.sendNotification(ApplicationFacade.EVENT_DATA_HOMEPIC_POST, this);
		}
		
		private function __mouseRollEventHandle(e:MouseEvent):void 
		{
			switch(e.type) {
				case MouseEvent.ROLL_OVER:
					TweenMax.to(iTxt, 0.5, { glowFilter: { color:0x00ff00, alpha:1, blurX:3, blurY:3, ease:Quad.easeOut } } );
					TweenMax.to(dTxt, 0.5, { glowFilter: { color:0x00ff00, alpha:1, blurX:3, blurY:3, ease:Quad.easeOut } } );
					
					TweenMax.to(background, 0.5, { tint:0x6cff00 ,ease:Quad.easeOut } );
					TweenMax.to(eff, 0.5, { scaleX:0.95, scaleY:0.9,alpha:0.7, ease:Bounce.easeOut } );
					TweenMax.to(infoPanel, 0.5, { width:268, ease:Quad.easeOut } );
					TweenMax.to(this, 0.5, { glowFilter: { color:0x000000, alpha:0.6, blurX:5, blurY:5, ease:Quad.easeOut }} );
					
				break;
				case MouseEvent.ROLL_OUT:
					TweenMax.to(iTxt, 0.5, { glowFilter: { color:0x339900, alpha:0, blurX:0, blurY:0, ease:Quad.easeOut } } );
					TweenMax.to(dTxt, 0.5, { glowFilter: { color:0x339900, alpha:0, blurX:0, blurY:0, ease:Quad.easeOut } } );
					
					TweenMax.to(background, 0.5, { tint:0xd5dadd,ease:Cubic.easeOut} );
					TweenMax.to(eff, 0.5, { scaleX:1, scaleY:1,alpha:0, ease:Back.easeIn } );
					TweenMax.to(infoPanel, 0.5, { width:253 } );
					TweenMax.to(this, 0.5, { glowFilter: { color:0xffffff, alpha:0, blurX:0, blurY:0, ease:Quad.easeIn }} );
				break;
			}
		}
	}

}